package top.drinyeit.nongmao.fragment;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.scu.miomin.shswiperefresh.core.SHSwipeRefreshLayout;

import top.drinyeit.nongmao.R;
import top.drinyeit.nongmao.activity.QccodePayActivity;
import top.drinyeit.nongmao.activity.SearchActivity;
import top.drinyeit.nongmao.base.LazyFragment;
import top.drinyeit.nongmao.presenter.HomePresenter;
import top.drinyeit.nongmao.view.CarouselView;

import static android.app.Activity.RESULT_CANCELED;
import static android.app.Activity.RESULT_OK;

/**
 * Created by Administrator on 2017/11/10.
 */

public class HomeFragment extends LazyFragment implements View.OnClickListener {

    private static int REQUEST_CODE = 5;
    private static final int REQ_CODE_PERMISSION = 0x1111;

    public CarouselView carouselView;
    private LinearLayout bigSearch;
    public RecyclerView moreRecyclerView;
    public RecyclerView iconRecycle;
    private TextView hot_search;
    private ImageView home_scan;
    public SHSwipeRefreshLayout swipeRefreshLayout;
    int statusBarHeight1 = -1;//状态栏高度

    private HomePresenter homePresenter;


    @Override
    protected int attachLayoutRes() {
        return R.layout.frag_home;
    }

    @Override
    protected void initViews() {
        bigSearch = mRootView.findViewById(R.id.bigSearch);
        hot_search = mRootView.findViewById(R.id.hot_search);
        home_scan = mRootView.findViewById(R.id.home_scan);
        carouselView = mRootView.findViewById(R.id.carouselView);
        moreRecyclerView = mRootView.findViewById(R.id.moreRecyclerView);
        iconRecycle = mRootView.findViewById(R.id.iconRecycle);
        swipeRefreshLayout = mRootView.findViewById(R.id.srl_home);
        setTitle(false);
    }

    private void initRecyclerData() {
//        List<CategoryGoodsDetailsBean> array = new ArrayList<>();
//        moreRecyclerView.setAdapter(new HomeClassfyAdapter(getActivity(),array));
    }

    public void initListener() {
        bigSearch.setOnClickListener(this);
        home_scan.setOnClickListener(this);
        swipeRefreshLayout.setLoadmoreEnable(false);
        swipeRefreshLayout.setOnRefreshListener(new SHSwipeRefreshLayout.SHSOnRefreshListener() {
            @Override
            public void onRefresh() {
                homePresenter.getApp();
            }

            @Override
            public void onLoading() {

            }

            @Override
            public void onRefreshPulStateChange(float v, int i) {

            }

            @Override
            public void onLoadmorePullStateChange(float v, int i) {

            }
        });
        initRecyclerData();
    }

    @Override
    protected void lazyLoad() {
        homePresenter = new HomePresenter(this);
        homePresenter.getApp();
        initListener();
    }


    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.bigSearch:
                intent.setClass(getActivity(), SearchActivity.class);
                startActivity(intent);
                break;
            case R.id.home_scan:
                if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                    // Do not have the permission of camera, request it.
                    ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, REQ_CODE_PERMISSION);
                } else {
                    // Have gotten the permission
                    startCaptureActivityForResult();
                }
                break;
        }
    }

    private void startCaptureActivityForResult() {
        Intent intent = new Intent(getActivity(), com.acker.simplezxing.activity.CaptureActivity.class);
        Bundle bundle = new Bundle();
        bundle.putBoolean(com.acker.simplezxing.activity.CaptureActivity.KEY_NEED_BEEP, com.acker.simplezxing.activity.CaptureActivity.VALUE_BEEP);
        bundle.putBoolean(com.acker.simplezxing.activity.CaptureActivity.KEY_NEED_VIBRATION, com.acker.simplezxing.activity.CaptureActivity.VALUE_VIBRATION);
        bundle.putBoolean(com.acker.simplezxing.activity.CaptureActivity.KEY_NEED_EXPOSURE, com.acker.simplezxing.activity.CaptureActivity.VALUE_NO_EXPOSURE);
        bundle.putByte(com.acker.simplezxing.activity.CaptureActivity.KEY_FLASHLIGHT_MODE, com.acker.simplezxing.activity.CaptureActivity.VALUE_FLASHLIGHT_OFF);
        bundle.putByte(com.acker.simplezxing.activity.CaptureActivity.KEY_ORIENTATION_MODE, com.acker.simplezxing.activity.CaptureActivity.VALUE_ORIENTATION_AUTO);
        bundle.putBoolean(com.acker.simplezxing.activity.CaptureActivity.KEY_SCAN_AREA_FULL_SCREEN, com.acker.simplezxing.activity.CaptureActivity.VALUE_SCAN_AREA_FULL_SCREEN);
        bundle.putBoolean(com.acker.simplezxing.activity.CaptureActivity.KEY_NEED_SCAN_HINT_TEXT, com.acker.simplezxing.activity.CaptureActivity.VALUE_SCAN_HINT_TEXT);
        intent.putExtra(com.acker.simplezxing.activity.CaptureActivity.EXTRA_SETTING_BUNDLE, bundle);
        startActivityForResult(intent, com.acker.simplezxing.activity.CaptureActivity.REQ_CODE);
    }

    //需要在activoty里重写
//    @Override
//    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//        switch (requestCode) {
//            case REQ_CODE_PERMISSION: {
//                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//                    // User agree the permission
//                    startCaptureActivityForResult();
//                } else {
//                    // User disagree the permission
////                    Toast.makeText(this, "You must agree the camera permission request before you use the code scan function", Toast.LENGTH_LONG).show();
//                    showToastMsg("请打开相机权限，设置-权限-" + R.string.app_name +  "-打开相机权限");
//                }
//            }
//            break;
//        }
//    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case com.acker.simplezxing.activity.CaptureActivity.REQ_CODE:
                switch (resultCode) {
                    case RESULT_OK:
//                        tvResult.setText(data.getStringExtra(CaptureActivity.EXTRA_SCAN_RESULT));  //or do sth
                        if (data.getStringExtra(com.acker.simplezxing.activity.CaptureActivity.EXTRA_SCAN_RESULT) != null) {
//                            showToastMsg(data.getStringExtra(com.acker.simplezxing.activity.CaptureActivity.EXTRA_SCAN_RESULT));
                            System.out.println("qccode:" + data.getStringExtra(com.acker.simplezxing.activity.CaptureActivity.EXTRA_SCAN_RESULT));
                            Intent intent = new Intent(getActivity(), QccodePayActivity.class);
                            if (data.getStringExtra(com.acker.simplezxing.activity.CaptureActivity.EXTRA_SCAN_RESULT).contains("avatar")) {
                                intent.putExtra("qccode", data.getStringExtra(com.acker.simplezxing.activity.CaptureActivity.EXTRA_SCAN_RESULT));
                                startActivity(intent);
                            } else {
                                showToastMsg("不是有效的二维码");
                            }
                        } else {
                            showToastMsg("请扫描正确的付款码");
                        }
                        break;
                    case RESULT_CANCELED:
                        if (data != null) {
                            // for some reason camera is not working correctly
//                            tvResult.setText(data.getStringExtra(CaptureActivity.EXTRA_SCAN_RESULT));
                            showToastMsg(data.getStringExtra(com.acker.simplezxing.activity.CaptureActivity.EXTRA_SCAN_RESULT));
                        }
                        break;
                }
                break;
        }
    }

    public CarouselView getCarouselView() {
        return carouselView;
    }

}
